package com.liveneo.adapter.mobile.web.controller;

import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Lists;
import com.liveneo.adapter.base.entity.PrpmMobilePushMsg;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.MobileSendMessageDto;
import com.liveneo.adapter.mobile.model.request.MsgListRequest;
import com.liveneo.adapter.mobile.model.request.UpdateStatesRequest;
import com.liveneo.adapter.mobile.model.response.MsgListResponse;
import com.liveneo.adapter.mobile.service.MobilePushService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.AppException;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;

@Controller
@RequestMapping("/mobile")
public class MobilePushController extends BaseController {
    @Autowired
    private MobilePushService mobilePushService;

    @RequestMapping("getMsgList.action")
    @ResponseBody
    public String getMsgList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "getMsgList入参=" + loguuid;
        MsgListRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, MsgListRequest.class);
        LogUtils.debug("获取推送消息列表， 传入参数:" + request);
        MsgListResponse response = null;
        String result = "";
        try {
            // 获取列表
            List<PrpmMobilePushMsg> msgList = mobilePushService.getMsgList(request);
            // 转化结果
            List<MobileSendMessageDto> prpmSendMessagelist = Lists.newArrayList();
            for (PrpmMobilePushMsg msg : msgList) {
                MobileSendMessageDto dto = new MobileSendMessageDto();
                BeanUtils.copyProperties(dto, msg);
                // TODO 待确认是否是flag字段
                dto.setIsRead(msg.getFlag());
                dto.setUuid(msg.getId());
                prpmSendMessagelist.add(dto);
            }
            // 返回参数
            response = MobileReturnCodeSupport
                    .buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null, MsgListResponse.class);
            response.setPrpmSendMessagelist(prpmSendMessagelist);
            response.setPageNo(request.getPageNo());
            response.setPageSize(request.getPageSize());
            response.setCondition(request.getCondition());
        } catch (AppException e) {
            LogUtils.error("获取推送消息列表时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode(), MsgListResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取推送消息列表时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    MsgListResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    // @RequestMapping("callback.action")
    // @ResponseBody
    // public String callback(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
    // String loguuid = UUIDUtils.getUUIDStr();
    // String logTitle = "callback入参=" + loguuid;
    // CallbackMsgRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CallbackMsgRequest.class);
    // LogUtils.debug("推送消息回调， 传入参数:" + request);
    // BaseResponse response = null;
    // String result = "";
    // try {
    // mobilePushService.callbackMsg(request);
    // response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS,
    // MobileReturnCodeSupport.SYS_CODE, null);
    // } catch (AppException e) {
    // LogUtils.error("推送消息回调时出错，错误信息:", e);
    // response = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE,
    // e.getDetailCode());
    // } catch (Exception e) {
    // LogUtils.error("推送消息回调时出错，错误信息:", e);
    // response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR,
    // MobileReturnCodeSupport.SYS_CODE, null);
    // } finally {
    // result = JsonUtils.toJson(response);
    // // 日志记录响应json
    // if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
    // LogUtils.info("返回结果【" + result + "】");
    // } else {
    // LogUtils.error("返回结果【" + result + "】");
    // }
    // result = EncryptUtil.encodeBase64(result);
    // }
    // return result;
    // }
    @RequestMapping("updateStates.action")
    @ResponseBody
    public String updateStates(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "updateStates入参=" + loguuid;
        UpdateStatesRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, UpdateStatesRequest.class);
        LogUtils.debug("更新消息状态， 传入参数:" + request);
        BaseResponse response = null;
        String result = "";
        try {
            mobilePushService.updateStates(request);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null);
        } catch (AppException e) {
            LogUtils.error("更新消息状态时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildBaseReponse(e.getCode(), MobileReturnCodeSupport.SYS_CODE, e.getDetailCode());
        } catch (Exception e) {
            LogUtils.error("更新消息状态时出错，错误信息:", e);
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }
}
